public class LinkedList implements IList {

    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }


    private void checkPos(int pos) {
        if(pos < 0 || pos > size()){
            throw new CheckPosException("下标异常"+pos);
        }
    }
    private ListNode searchIndex(int pos){
        ListNode cur = head;
        while(pos > 0) {
            cur = cur.next;
            pos--;
        }
        return cur;
    }
    @Override
    public void addIndex(int index, int data) {
        checkPos(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = searchIndex(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;

    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        isEmpty();
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){
                        head.prev =null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    } else{
                        last = last.prev;
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }

    }

    @Override
    public void removeAllKey(int key) {
        isEmpty();
        


    }

    @Override
    public int size() {
        if(head == null) {
            return 0;
        }
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    private void isEmpty(){
        if(head == null){
            throw new IsEmptyException("该链表为空,无法进行该操作");
        }
    }
    @Override
    public void display() {
        isEmpty();
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        head =null;
        last = null;
    }
}
